package lanou3g.myfirstapp36kr.fragment;

import android.animation.ObjectAnimator;
import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.PopupWindow;

import lanou3g.myfirstapp36kr.R;
import lanou3g.myfirstapp36kr.activity.FindgoodprojectActivity;
import lanou3g.myfirstapp36kr.activity.SearchActivity;
import lanou3g.myfirstapp36kr.adapter.FragmentMoneyViewPagerAdapter;
import lanou3g.myfirstapp36kr.base.BaseFragment;


/**
 * Created by dllo on 16/8/2.
 */
public class FragmentMoney extends BaseFragment implements OnClickListener {
    private ViewPager viewPager;
    private TabLayout tabLayout;
    private ImageView imageViewPresent;
    private LayoutInflater inflater;
    private PopupWindow popupWindow;
    private ImageView imageViewPresentPic;

    @Override
    protected int getLayout()
    {
        return R.layout.fragment_money;
    }

    @Override
    protected void initView(View view) {
        viewPager = (ViewPager) view.findViewById(R.id.view_pager_money);
        tabLayout = (TabLayout) view.findViewById(R.id.tablayout_money);
        imageViewPresent = (ImageView) view.findViewById(R.id.iv_present);
        inflater = LayoutInflater.from(this.getActivity());
        popupWindow = new PopupWindow(this.getActivity());
        popupWindow.setAnimationStyle(R.style.popwin_anim_style);


    }

    @Override
    protected void initData() {
        getActivity().findViewById(R.id.iv_search_money).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(FragmentMoney.this.getActivity(), SearchActivity.class);
                startActivity(intent);
            }
        });
        FragmentMoneyViewPagerAdapter adapter = new FragmentMoneyViewPagerAdapter(getChildFragmentManager());
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        imageViewPresent.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        if (popupWindow.isShowing()){
            popupWindow.dismiss();
            ObjectAnimator oa = ObjectAnimator.ofFloat(imageViewPresent,"translationX",0,50,-61,64,21,-21,50,-61,54,21,0);
            oa.setDuration(700);
            oa.start();
        }else {
            showPop();
        }

    }
    private void showPop(){
        popupWindow.setWidth(800);
        popupWindow.setHeight(900);
        View view = inflater.inflate(R.layout.pop_present,null);
        imageViewPresentPic = (ImageView) view.findViewById(R.id.iv_present_pic);
        imageViewPresentPic.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(FragmentMoney.this.getActivity(),FindgoodprojectActivity.class);
                startActivity(intent);
                popupWindow.dismiss();
            }
        });
        View parant = inflater.inflate(R.layout.fragment_money,null);
        popupWindow.setContentView(view);

        popupWindow.showAtLocation(parant, Gravity.CENTER,0,0);

    }
}
